﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PeiYue.Common.HttpContextUser;
using PeiYue.IServices.BASE;
using PeiYue.Model;
using PeiYue.Model.Dto.Sys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace PeiYue.Api.Controllers.BASE
{
    public class BaseController<TEntity> : ControllerBase, IBaseController<TEntity> where TEntity : class, new()
    {
        public IBaseServices<TEntity> BaseSer;
        IUser _user;
        public BaseController(IBaseServices<TEntity> _BaseSer, IUser user)
        {
            BaseSer = _BaseSer;
            _user = user;
        }

        ///// <summary>
        ///// 更新
        ///// </summary>
        ///// <param name="entitys"></param>
        ///// <returns></returns>
        //[HttpPost]
        //[ApiExplorerSettings(IgnoreApi = true)]
        //public async Task<MessageModel<string>> Updates([FromBody] IList<TEntity> entitys)
        //{
        //    var data = new MessageModel<string>();
        //    if (entitys.Count() > 0)
        //    {
        //        //userDetail.tdIsDelete = true;
        //        data.success = await BaseSer.Updates(entitys);
        //        if (data.success)
        //        {
        //            data.msg = "成功";
        //        }
        //    }
        //    return data;
        //}
        /// <summary>
        /// 启用
        /// </summary>
        /// <param name="updateStatusDto"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize(Permissions.Name)]
        public async Task<MessageModel<string>> Enable([FromBody] UpdateStatusDto updateStatusDto)
        {
            return await UpdateStatus(updateStatusDto.ids, (int)CommonEnum.Status.Enable);
        }
        /// <summary>
        /// 禁用
        /// </summary>
        /// <param name="updateStatusDto"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize(Permissions.Name)]
        public async Task<MessageModel<string>> Disable([FromBody] UpdateStatusDto updateStatusDto)
        {
            return await UpdateStatus(updateStatusDto.ids, (int)CommonEnum.Status.Disable);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="updateStatusDto"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize(Permissions.Name)]
        public async Task<MessageModel<string>> Delete([FromBody] UpdateStatusDto updateStatusDto)
        {
            return await UpdateStatus(updateStatusDto.ids, (int)CommonEnum.Status.Delete);
        }
        private async Task<MessageModel<string>> UpdateStatus(IList<long> ids, int status)
        {
            var data = new MessageModel<string>();
            var types = typeof(TEntity);
            var table = (SqlSugar.SugarTable)types.GetCustomAttributes(typeof(SqlSugar.SugarTable), false).FirstOrDefault();
            string id = string.Join(",", ids.ToArray());
            string sql = $"update {table.TableName} set status=@status,update_user=@update_user,update_time=@update_time where id in (" + id + ")";
            await BaseSer.ExecuteCommand(sql, new { status = status, update_user = _user.ID, update_time = DateTime.Now });
            data.success = true;
            data.msg = "操作成功";
            return data;
        }
    }
}
